<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="Vendor" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vendor">
              <a-input :disabled="isDisabled" v-model="model.vendor" placeholder="请输入Vendor"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="ICT P/N" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ict">
              <j-dict-select-tag
                v-model="model.ict"
                dictCode="ictpn"
                placeholder="请输入ICT P/N"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="D/C" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dc">
              <a-input v-model="model.dc" placeholder="请输入D/C(示例：20231018)"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="QTY" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qty">
              <a-input v-model="model.qty" placeholder="请输入QTY"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="V/C" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vc">
              <a-input :disabled="isDisabled" v-model="model.vc" placeholder="请输入V/C"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="P/O" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="po">
              <a-input v-model="model.po" placeholder="请输入P/O"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Lot No" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lotNo">
              <a-input v-model="model.lotNo" placeholder="请输入Lot No"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="MSD" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="msd">
              <a-input v-model="model.msd" placeholder="请输入MSD"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="X Type" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemXType">
              <a-input v-model="model.itemXType" placeholder="请输入X Type"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="X Qty" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemXQty">
              <a-input v-model="model.itemXQty" placeholder="请输入X Qty"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Cavity" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cavity">
              <a-input v-model="model.cavity" placeholder="请输入Cavity"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="BIN" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bin">
              <a-input v-model="model.bin" placeholder="请输入BIN"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="MPN" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mpn">
              <a-input v-model="model.mpn" placeholder="请输入MPN"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'RawMaterialPackagingForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        isDisabled: true,
        model:{
          vendor: 'Shenzhen Changhong Technology Co.,Ltd.',
          vc: '1012867'

         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           vendor: [
              { required: true, message: '请输入Vendor!'},
           ],
           ict: [
              { required: true, message: '请输入ICT P/N!'},
           ],
           dc: [
              { required: true, message: '请输入D/C!'},
              { max: 8, message: '长度不超过8!'},
              { min: 8, message: '长度不小于8!'},   
              //{ type: 'number',message: '必须填入数字！'}
              { pattern: new RegExp(/^[1-9]\d*$/, "g"),message: '请遵循示例格式！'}
           ],
           qty: [
              { required: true, message: '请输入QTY!'},
           ],
           vc: [
              { required: true, message: '请输入V/C!'},
           ],
           po: [
              { required: true, message: '请输入P/O!'},
           ],
        },
        url: {
          add: "/ch/rawMaterialPackaging/add",
          edit: "/ch/rawMaterialPackaging/edit",
          queryById: "/ch/rawMaterialPackaging/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>